/*
多线程
    两种方法实现
    1.实现Runnable接口
        创建一个类，说明使用了Runnable接口，实现其功能(主要是run()函数)
        class NewThread implements Runnable{
            boolean sta = true;
            NewThread(){
            }
            public void run(){
                while(sta){
                    do something
                }
            }
        }
     2.拓展Thread
        创建一个类继承于Thread，实现run()函数
        class RunThread extends Thread{
            boolean sta = true;
            RunThread(){
            }
            public void run(){
                while(sta){
                    do something
                }
            }
        }
      3.线程同步
        thread类的join方法可以阻塞当前线程，直至目标线程结束
 */
public class Project {
    public static void main(String[] args) throws InterruptedException {

        Thread thread = Thread.currentThread();
        thread.setPriority(5);
        System.out.println("thread.getPriority():"+thread.getPriority());
        System.out.println("拓展Thread RunThread");
        RunThread runThread = new RunThread();

        System.out.println("实现Runnable接口 NewThread");
        NewThread thread1 = new NewThread("zero",500);
        thread1.init();
        NewThread thread2 = new NewThread("one",600);
        thread2.init();
        NewThread thread3 = new NewThread("two",700);
        thread3.init();
        NewThread thread4 = new NewThread("three",800);
        thread4.init();
        System.out.println("使用join()阻塞主线程，实现同步");
        thread4.t.join();

        System.out.println("主线程"+"运行结束");

    }
}

class RunThread extends Thread{
    RunThread(){
        super("RunThread");
        System.out.println("构造子线程："+this);
        start();
    }
    public void run(){
        System.out.println(getName()+"开始运行");
        for (int i = 0;i<10;i++){
            System.out.println(getName()+" "+i);
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                System.out.println(e);
            }
        }
        System.out.println(getName()+"结束运行");
    }
}

class NewThread implements Runnable{
    Thread t;
    private volatile boolean sta = false;
    int delayTime = 1000;
    public boolean setSta(boolean sta){
        this.sta = sta;
        return this.sta;
    }
    public boolean getSta(){
        return sta;
    }
    public NewThread() {
        t = new Thread(this,"NewThread");
        System.out.println(t);
    }
    public NewThread(String name) {
        t = new Thread(this,name);
        System.out.println(t);
    }
    public NewThread(String name, int delayTime) {
        this(name);
        this.delayTime = delayTime;
    }
    public void init(){
        setSta(true);
        t.start();
        System.out.println(t.getName()+"开始运行");
    }
    public void run(){
        while (sta){
            for (int i = 0;i<10;i++){
                System.out.println(t.getName()+" "+i);
                try {
                    Thread.sleep(delayTime);
                } catch (InterruptedException e) {
                    System.out.println(e);
                }
            }
            setSta(false);
        }
        System.out.println(t.getName()+"结束运行");
    }

    public void join() {
    }
}
